﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using AutoLoL.Entities;
using AutoLoL.Logic;
using AutoLoL.Logic.Factories;
using AutoLoL.Logic.Masteries;
using AutoLoL.Logic.Masteries.Importers;
using AutoLoL.Logic.Utils;
using AutoLoL.Shared;

namespace AutoLoL.Dialogs
{
    /// <summary>
    /// Interaction logic for ImportDialog.xaml
    /// </summary>
    public partial class ImportDialog : Window
    {
        private BackgroundWorker _importer;
        private List<IMasteryImporter> _importers;

        public string ImportResult { get; private set; }

        public ImportDialog(Window owner)
        {
            InitializeComponent();
            Owner = owner;

            _importers = new List<IMasteryImporter>();
            _importers.Add(new SimpleMasteryImporter());
            _importers.Add(new SoloMidMasteryImporter());
            _importers.Add(new MobafireMasteryImporter());

            SourceComboBox.ItemsSource = _importers;

            ImportButton.IsDefault = true;
            CancelButton.IsCancel = true;

            _importer = new BackgroundWorker();
            _importer.WorkerReportsProgress = true;
            _importer.DoWork += new DoWorkEventHandler(Importer_DoWork);
            _importer.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Importer_RunWorkerCompleted);
        }

        /// <summary>
        /// Selects an importer based on users input
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InputTextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            foreach (IMasteryImporter importer in _importers)
            {
                if (importer.Select(InputTextBox.Text))
                {
                    SourceComboBox.SelectedItem = importer;
                    break;
                }
            }
        }

        private void ImportButton_Click(object sender, RoutedEventArgs e)
        {
            SourceComboBox.IsEnabled = false;
            InputTextBox.IsEnabled = false;
            ImportButton.IsEnabled = false;

            LoadingStackPanel.Visibility = Visibility.Visible;

            IMasteryImporter importer = SourceComboBox.SelectedItem as IMasteryImporter;
            string input = InputTextBox.Text;

            dynamic param = new
            {
                Importer = importer,
                Input = input
            };

            _importer.RunWorkerAsync(param);
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            ImportResult = null;
            Close();
        }

        private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
            e.Handled = true;
        }

        private void Importer_DoWork(object sender, DoWorkEventArgs e)
        {
            dynamic param = e.Argument;
            IMasteryImporter importer = param.Importer;
            string input = param.Input;

            try
            {
                e.Result = importer.Import(input);
            }
            catch
            {
                e.Result = null;
            }
        }

        private void Importer_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            ImportResult = e.Result as string;

            Close();
        }
    }
}
